package com.ruoyi.eam.service.impl;

import java.util.List;

import com.ruoyi.eam.doman.EamAsset;
import com.ruoyi.eam.mapper.EamAssetMapper;
import com.ruoyi.eam.mapper.EamStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.mapper.EamStockDMapper;
import com.ruoyi.eam.doman.EamStockD;
import com.ruoyi.eam.service.IEamStockDService;

/**
 * 入库从Service业务层处理
 *
 * @author xl
 * @date 2024-06-27
 */
@Service
public class EamStockDServiceImpl implements IEamStockDService
{
    @Autowired
    private EamStockDMapper eamStockDMapper;
    @Autowired
    private EamStockMapper stockMapper;
    @Autowired
    private EamAssetMapper assetMapper;

    /**
     * 查询入库从
     *
     * @param stokId 入库从主键
     * @return 入库从
     */
    @Override
    public EamStockD selectEamStockDByStokId(Long stokId)
    {
        return eamStockDMapper.selectEamStockDByStokId(stokId);
    }

    /**
     * 查询入库从列表
     *
     * @param eamStockD 入库从
     * @return 入库从
     */
    @Override
    public List<EamStockD> selectEamStockDList(EamStockD eamStockD)
    {
        return eamStockDMapper.selectEamStockDList(eamStockD);
    }

    /**
     * 新增入库从
     *
     * @param eamStockD 入库从
     * @return 结果
     */
    @Override
    public int insertEamStockD(EamStockD eamStockD)
    {
        //获取最后一条入库id
        Long stockIdLast = stockMapper.selectEamStockList(null).get(stockMapper.selectEamStockList(null).size() - 1).getStockId();
        //获取最后一条资产的id
        EamAsset asset = assetMapper.selectEamAssetList(null).get(assetMapper.selectEamAssetList(null).size()-1);
        eamStockD.setStokId(stockIdLast);
        eamStockD.setCount(asset.getAssetNum());
        //登记入库
        if(eamStockD.getAssetId() == null){
            eamStockD.setAssetId(asset.getAssetId());
            return eamStockDMapper.insertEamStockD(eamStockD);
        }else{
            //修改入库
            List<EamStockD> list = eamStockDMapper.selectEamStockDByAssetId(eamStockD.getAssetId());
            System.out.println("list:"+list);
            if(list.size() != 0){
                //资产已有入库记录
                return 0;
            }else{
                //修改入库
                eamStockD.setAssetId(eamStockD.getAssetId());
                return eamStockDMapper.insertEamStockD(eamStockD);
            }
        }

    }

    /**
     * 修改入库从
     *
     * @param eamStockD 入库从
     * @return 结果
     */
    @Override
    public int updateEamStockD(EamStockD eamStockD)
    {
        return eamStockDMapper.updateEamStockD(eamStockD);
    }

    /**
     * 批量删除入库从
     *
     * @param stokIds 需要删除的入库从主键
     * @return 结果
     */
    @Override
    public int deleteEamStockDByStokIds(Long[] stokIds)
    {
        return eamStockDMapper.deleteEamStockDByStokIds(stokIds);
    }

    /**
     * 删除入库从信息
     *
     * @param stokId 入库从主键
     * @return 结果
     */
    @Override
    public int deleteEamStockDByStokId(Long stokId)
    {
        return eamStockDMapper.deleteEamStockDByStokId(stokId);
    }
}
